<template>
  <div class="category" data-title="文章分类 - MBlog" v-title>
    <ul>
      <li :key="c.id" v-for="c in categories">
        <div class="category-card">
          <a @click="view(c.id)">
            <img :src="c.avatar" />
            <h4>{{c.categoryName}}</h4>
            <p v-if="c.description">{{c.description}}</p>
            <p v-else>暂无描述</p>
          </a>
          <div>
            <span>{{c.articleNum}} 文章</span>
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
import { getAllCategories } from '@/api/category'
export default {
  data () {
    return {
      loading: false,
      categories: []
    }
  },
  mounted () {
    this.getAllCategories()
  },
  methods: {
    getAllCategories () {
      getAllCategories()
        .then(data => {
          this.categories = data.data
        })
        .catch(error => {
          if (error !== 'error') {
            this.$message({
              type: 'error',
              message: '文章加载失败!',
              showClose: true
            })
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    view (id) {
      this.$router.push({ name: 'articleByCategory', params: { id } })
    }
  }
}
</script>

<style>
.category {
  line-height: 15px;
  text-align: left;
}
.category li {
  text-align: center;
  display: inline-block;
  list-style-type: none;
  width: 25%;
  margin: 32px 22px;
}

.category-card {
  border: 1px solid #e1e1e1;
  padding: 10px;
  text-align: center;
}
.category-card > div {
  font-size: 12px;
  color: #969696;
  text-align: center;
}
.category-card img {
  width: 70px;
  height: 60px;
  margin: -40px 0 10px;
}
.category-card h4 {
  margin: 15px 0;
  text-align: center;
}
.category-card p {
  font-size: 13px;
  line-height: 13px;
  height: 26px;
  margin-bottom: 15px;
  text-align: center;
}
</style>
